﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemBUS;

namespace NangLuongTieuDiemGUI.NangLuongTieuDiemForms
{
    public partial class frmSaoLuuVaPhucHoiDuLieu : Form
    {
        string thongBao;
        public frmSaoLuuVaPhucHoiDuLieu()
        {
            InitializeComponent();
            saoLuuFile.InitialDirectory = Application.StartupPath;
            phucHoiFile.InitialDirectory = Application.StartupPath;
        }

        #region "[timer1_Tick]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// timer1_Tick
        /// </summary>
        /// <param name="_strLocation"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                progressBar1.Visible = true;
                progressBar1.Value += 5;
                if (progressBar1.Value == 100)
                {
                    timer1.Enabled = false;
                    MessageBox.Show(thongBao, "Thông báo:");
                    progressBar1.Value = 0;
                    progressBar1.Visible = false;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("timer1_Tick", ex);
            }
        }
        #endregion

        #region "[sự kiên radioButton SaoLuu checked changed]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// sự kiên radioButton SaoLuu checked changed
        /// </summary>
        /// <param name="_strLocation"></param>
        private void rbSaoLuu_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbSaoLuu.Checked == true)
                {
                    pictureBox1.Image = NangLuongTieuDiemGUI.Properties.Resources.database_down;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("rbSaoLuu_CheckedChanged", ex);
            }
        }
        #endregion

        #region "[sự kiên radioButton PhucHoi checked changed]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// sự kiên radioButton PhucHoi checked changed
        /// </summary>
        /// <param name="_strLocation"></param>
        private void rbPhucHoi_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbPhucHoi.Checked == true)
                {
                    pictureBox1.Image = NangLuongTieuDiemGUI.Properties.Resources.database_up;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("rbPhucHoi_CheckedChanged", ex);
            }
        }
        #endregion

        #region "[btnDuongDan]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// btnDuongDan
        /// </summary>
        /// <param name="_strLocation"></param>
        private void btnDuongDan_Click(object sender, EventArgs e)
        {
            try
            {

                if (rbSaoLuu.Checked == true)
                {
                    saoLuuFile.FileName = "NangLuongTieuDiem_" + DateTime.Now.ToString("dd.MM.yyyy") + ".bak";
                    saoLuuFile.ShowDialog();
                }
                if (rbPhucHoi.Checked == true)
                {
                    phucHoiFile.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnDuongDan_Click", ex);
            }
        }
        #endregion

        #region "[sự kiên SaveFileDialog saoLuuFile_FileOK]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// sự kiên SaveFileDialog_FileOK
        /// </summary>
        /// <param name="_strLocation"></param>
        private void saoLuuFile_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                txtDuongDan.Text = saoLuuFile.FileName;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("saoLuuFile_FileOk", ex);
            }
        }
        #endregion

        #region "[sự kiên OpenFileDialog phucHoiFile_FileOK]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// sự kiên OpenFileDialog phucHoiFile_FileOK
        /// </summary>
        /// <param name="_strLocation"></param>
        private void phucHoiFile_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                txtDuongDan.Text = phucHoiFile.FileName;
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("phucHoiFile_FileOk", ex);
            }

        }
        #endregion

        #region "[btnBatDau]"
        /// <summary>
        /// Người tạo: Cao Hoang An - 1241301
        /// btnBatDau
        /// </summary>
        /// <param name="_strLocation"></param>
        private void btnBatDau_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(saoLuuFile.FileName) && string.IsNullOrEmpty(phucHoiFile.FileName))
                {

                    MessageBox.Show("Hãy chọn đường dẫn.!", "Thông báo:");
                }
                else
                {
                    if (rbSaoLuu.Checked == true)
                    {

                        if (NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.Backup(txtDuongDan.Text) == true)
                        {
                            timer1.Enabled = true;
                            thongBao = "Đã sao lưu dữ liệu thành công.";
                        }
                        else
                        {
                            MessageBox.Show("Sao lưu dữ liệu thất bại.\nCó thể sql của bạn ko hổ trợ BackUp", "Thông báo:");
                        }
                    }
                    else
                    {
                        if (NangLuongTieuDiemBUS.NangLuongTieuDiem_Management.Restore(txtDuongDan.Text) == true)
                        {
                            timer1.Enabled = true;
                            thongBao = "Đã phục hồi dữ liệu thành công.";
                        }
                        else
                        {
                            MessageBox.Show("Khôi phục dữ liệu thất bại.\nCó thể sql của bạn ko hổ trợ Restore hoặc sai file", "Thông báo:");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnBatDau_Click", ex);
            }
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "frmSaoLuuVaPhucHoiDuLieu_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            NangLuongTieuDiem_Management.LoiHeThong_Insert(lhtDto);
            MessageBox.Show(lhtDto.TenLoi);
        }
        #endregion
    }
}
